import { Entity, Column, Index, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../common/base/base.entity';
import { User } from './user.entity';
import { Product } from './product.entity';

@Entity('download_records')
@Index('idx_user_time', ['userId', 'downloadedAt'])
export class DownloadRecord extends BaseEntity {
  @Column({
    type: 'bigint',
    comment: '用户ID',
  })
  userId: string;

  @Column({
    type: 'bigint',
    comment: '商品ID',
  })
  productId: string;

  @Column({
    type: 'enum',
    enum: ['paid', 'membership', 'free'],
    comment: '下载类型',
  })
  type: 'paid' | 'membership' | 'free';

  @Column({
    type: 'enum',
    enum: ['free', 'vip', 'svip'],
    nullable: true,
    comment: '会员等级',
  })
  membershipLevel?: 'free' | 'vip' | 'svip';

  @Column({
    type: 'varchar',
    length: 50,
    nullable: true,
    comment: '下载IP',
  })
  ip?: string;

  @Column({
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
    comment: '下载时间',
  })
  downloadedAt: Date;

  // 关联
  @ManyToOne(() => User)
  @JoinColumn({ name: 'userId' })
  user: User;

  @ManyToOne(() => Product)
  @JoinColumn({ name: 'productId' })
  product: Product;
}
